package com.xiguaji.tiktok.v1.web.ins;

import cn.hutool.core.date.DateUtil;
import com.xiguaji.boot.toolkit.HttpUtil;
import com.xiguaji.tiktok.v1.ins.dic.service.DicService;
import com.xiguaji.tiktok.v1.ins.rank.dto.InsRkSearch;
import com.xiguaji.tiktok.v1.ins.rank.service.InsRankService;
import com.xiguaji.tiktok.v1.web.Tab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

@RestController
public class InsRankingController extends InsBaseController {

    @Autowired
    InsRankService insRankService;

    @Autowired
    DicService dicService;


    /**
     * 总排行榜分类国家
     * @param category
     * @param nation
     * @return
     */
    @RequestMapping(value = "/rank-{category}-influencers-{nation}",method = RequestMethod.GET)
    public ModelAndView influencersRank(@PathVariable String category,@PathVariable String nation){
        ModelAndView modelAndView = getModel(Tab.INS_RANK_BLOGGER.getValue());

        if(category.equals("All") && nation.equals("All")){
            return new ModelAndView("redirect:/instagram/rank-influencers");
        }
        modelAndView.setViewName("ins/rank/index");
        influencersRankModel(modelAndView,category,nation);
        return modelAndView;
    }

    /**
     * 总榜排行榜国家
     * @param nation
     * @return
     */
    @RequestMapping(value = "/rank-influencers-{nation}",method = RequestMethod.GET)
    public ModelAndView influencersNationRank(@PathVariable String nation){
        ModelAndView modelAndView = getModel(Tab.INS_RANK_BLOGGER.getValue());
        modelAndView.setViewName("ins/rank/index");
        influencersRankModel(modelAndView,"",nation);
        return modelAndView;
    }

    /**
     * 总榜排行榜分类
     * @param category
     * @return
     */
    @RequestMapping(value = "/rank-{category}-influencers",method = RequestMethod.GET)
    public ModelAndView influencersCategoryRank(@PathVariable String category){
        ModelAndView modelAndView = getModel(Tab.INS_RANK_BLOGGER.getValue());
        modelAndView.setViewName("ins/rank/index");
        influencersRankModel(modelAndView,category,"");
        return modelAndView;
    }

    /**
     * 总排行榜
     * @return
     */
    @RequestMapping(value = "/rank-influencers",method = RequestMethod.GET)
    public ModelAndView influencersRankAll(){
        ModelAndView modelAndView = getModel(Tab.INS_RANK_BLOGGER.getValue());
        modelAndView.setViewName("ins/rank/index");
        influencersRankModel(modelAndView,"","");
        return modelAndView;
    }

    private void influencersRankModel(ModelAndView modelAndView,String category,String nation){
        InsRkSearch followerSearch = new InsRkSearch();
        followerSearch.setCategoryName(category);

        String nationCode = nation;
        if(!StringUtils.isEmpty(nation)){
            nationCode = dicService.getCode(nation);
            followerSearch.setNation(nationCode);
        }
        followerSearch.setDate(insRankService.getUpdateDateCodeByFollowerRank(followerSearch.getCategoryName(),nationCode));

        if(followerSearch.getDate() == 0){
            followerSearch.setDate(Integer.parseInt(LocalDateTime.now().plusMonths(-1).format(DateTimeFormatter.ofPattern("yyyyMM"))));
        }
        modelAndView.addObject("category",category);
        modelAndView.addObject("nation",nation);
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        modelAndView.addObject("listData",insRankService.getFollowerRankPage(followerSearch));
        long updateTime = DateUtil.parse(String.valueOf(followerSearch.getDate()),"yyyyMM").getTime()/1000;
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(updateTime), ZoneId.systemDefault());
        modelAndView.addObject("updateTimeStr",localDateTime.format(DateTimeFormatter.ofPattern("MMM yyyy", Locale.ENGLISH)));
        modelAndView.addObject("updateTime",insRankService.getUpdateTimeByFollowerRank(followerSearch.getCategoryName(),nationCode)*1000);
        modelAndView.addObject("categories",dicService.getOverallCategory());
        modelAndView.addObject("nations",dicService.getNations(1));

        try{

            String url = HttpUtil.getRequest().getRequestURL().toString();
            url = super.serverDomain + url.substring(url.indexOf("instagram"));

            modelAndView.addObject("curUrl", URLDecoder.decode(url, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/follower-growth",method = RequestMethod.GET)
    public ModelAndView followerGrowthRank(){
        ModelAndView modelAndView = getModel(Tab.INS_RANK_BLOGGER_MORE_FOLLOWERS.getValue());
        modelAndView.setViewName("ins/follower/growth");
        modelAndView.addObject("categories",dicService.getOverallCategory());
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        modelAndView.addObject("category","");
        int dateCode = insRankService.getDayMaxDateCode("");
        InsRkSearch insRkSearch =new InsRkSearch();
        insRkSearch.setDate(dateCode);
        modelAndView.addObject("listData",insRankService.getFgDayRankPage(insRkSearch));
        modelAndView.addObject("updateTime",insRankService.getUpdateTimeByFgDayRank("")*1000);
        return modelAndView;
    }

    /**
     * 商业合作者
     * @return
     */
    @RequestMapping(value = "/rank-commercial-partner",method = RequestMethod.GET)
    public ModelAndView commercialPartnerRank(){
        ModelAndView modelAndView = getModel(Tab.INS_RANK_BLOGGER_COMMERCIAL.getValue());
        modelAndView.setViewName("ins/rank/parter");
        modelAndView.addObject("categories",dicService.getOverallCategory());
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        modelAndView.addObject("category","");
        int dateCode = insRankService.getMaxDateCodeByAdecWeekRank("");
        InsRkSearch insRkSearch = new InsRkSearch();
        insRkSearch.setDate(dateCode);
        modelAndView.addObject("listData",insRankService.getAdecWeekRankPage(insRkSearch));
        modelAndView.addObject("updateTime",insRankService.getUpdateTimeByAdecWeekRank("")*1000);
        return modelAndView;
    }

    /**
     * 全部商品排行榜
     * @return
     */
    @RequestMapping(value = "/product-ranking")
    public ModelAndView productRank(){
        return productRankBody("");
    }
    /**
     * 分类商品排行榜
     * @return
     */
    @RequestMapping(value = "/{category}-product-ranking")
    public ModelAndView productCategoryRank(@PathVariable String category){
        return productRankBody(category);
    }

    private ModelAndView productRankBody(String category){
        ModelAndView modelAndView = getModel(Tab.INS_PRODUCT_RANK.getValue());
        modelAndView.addObject("categories",dicService.findCommercialCategory());
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        modelAndView.addObject("category", category);
        InsRkSearch insRkSearch = new InsRkSearch();
        insRkSearch.setCategoryName(category);
        insRkSearch.setDate(insRankService.getProductNewestDateCode(insRkSearch));
        modelAndView.addObject("listData",insRankService.getProductRankPage(insRkSearch));
        modelAndView.addObject("updateTime",insRankService.getProductNewestUpdateDate(insRkSearch)*1000);
        modelAndView.setViewName("ins/rank/product/index");
        return modelAndView;
    }


    /**
     * 全部品牌排行榜
     * @return
     */
    @RequestMapping(value = "/brand-ranking")
    public ModelAndView brandRank(){
        return brandRankBody("");
    }
    /**
     * 分类品牌排行榜
     * @return
     */
    @RequestMapping(value = "/{category}-brand-ranking")
    public ModelAndView brandCategoryRank(@PathVariable String category){
        return brandRankBody(category);
    }

    private ModelAndView brandRankBody(String category){
        ModelAndView modelAndView = getModel(Tab.INS_BRAND_RANK.getValue());
        modelAndView.addObject("categories",dicService.findCommercialCategory());
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        modelAndView.addObject("category", category);
        InsRkSearch insRkSearch = new InsRkSearch();
        insRkSearch.setCategoryName(category);
        insRkSearch.setDate(insRankService.getBrandNewestDateCode(insRkSearch));
        modelAndView.addObject("listData",insRankService.getBrandDayRankPage(insRkSearch));
        modelAndView.addObject("updateTime",insRankService.getUpdateTimeByBrandDayRank(category)*1000);
        modelAndView.setViewName("ins/rank/brand/index");
        return modelAndView;
    }


    /**
     * 带货达人
     */
    @RequestMapping(value = "/rank-product-tagged-influencer")
    public ModelAndView commercialPartner(){
        ModelAndView modelAndView = getModel(Tab.INS_PRODUCT_TAGGED_RANK.getValue());
        modelAndView.addObject("categories",dicService.getOverallCategory());
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        InsRkSearch insRkSearch = new InsRkSearch();
        insRkSearch.setDate(insRankService.getBloggerBgsNewestDateCode(insRkSearch));
        modelAndView.addObject("listData",insRankService.getBloggerBgsDayRankPage(insRkSearch));
        modelAndView.addObject("updateTime",insRankService.getUpdateTimeByBloggerBgsDayRank("")*1000);
        modelAndView.setViewName("ins/rank/product/tagged/index");
        return modelAndView;

    }
}
